﻿#ifndef FORMSETTING_H
#define FORMSETTING_H

#include <QWidget>
#include <QItemDelegate>
#include <QLineEdit>
#include <QStandardItemModel>
#include <QToolButton>


//Delegate Read only
class ReadOnlyDelegate : public QItemDelegate
{
    Q_OBJECT
public:
    ReadOnlyDelegate(QObject *parent = 0): QItemDelegate(parent) { }
    QWidget *createEditor(QWidget*, const QStyleOptionViewItem &/*option*/,
                          const QModelIndex &/*index*/) const
    {
        return NULL;
    }
};


#include <qDebug>
class LineEditDelegate : public QItemDelegate
{
    Q_OBJECT
signals:
    void finishEdEdit(QString);

//public slots:
//    void slot_finishEdEdit(QString);

public:
    LineEditDelegate(QObject *parent = 0): QItemDelegate(parent){ }
    QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &/*option*/,
                          const QModelIndex &index) const;
//    {
//        QLineEdit *editor = new QLineEdit(parent);
//        QRegExp regExp("((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)");
//        editor->setValidator(new QRegExpValidator(regExp, parent));
//        editor->setAlignment(Qt::AlignCenter);
//        connect(editor,&QLineEdit::textChanged,[=]{
////            qDebug()<<"delegate.edit finished";
////            setEditorData(editor,index);
//        });
//        return editor;
//    }
    void setEditorData(QWidget *editor, const QModelIndex &index) const;
//    {
//        QString text = index.model()->data(index, Qt::EditRole).toString();
//        QLineEdit *lineEdit = static_cast<QLineEdit*>(editor);

//        lineEdit->setText(text);
//    }
    void setModelData(QWidget *editor, QAbstractItemModel *model,
                      const QModelIndex &index) const;
//    {
//        QLineEdit *lineEdit = static_cast<QLineEdit*>(editor);
//        QString text = lineEdit->text();
//        model->setData(index, text, Qt::EditRole);
//    }
    void updateEditorGeometry(QWidget *editor,
                              const QStyleOptionViewItem &option, const QModelIndex &/*index*/) const;
//    {
//        editor->setGeometry(option.rect);
//    }
};

//Delegate delete button
class DeleteButton : public QItemDelegate
{
    Q_OBJECT
public:
    DeleteButton(QObject *parent = 0): QItemDelegate(parent) { }
    QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &/*option*/,
                          const QModelIndex &/*index*/) const
    {
        QToolButton *deletebtn = new QToolButton(parent);
        deletebtn->setText(QStringLiteral("删除"));
        return deletebtn;
    }
    void setEditorData(QWidget *editor, const QModelIndex &index) const
    {
        QString text = index.model()->data(index, Qt::EditRole).toString();
        QToolButton *deletebtn = static_cast<QToolButton*>(editor);
//        int tindex = deletebtn->findText(text);
        deletebtn->setText(QStringLiteral("删除"));
        connect(deletebtn,&QToolButton::clicked,[=]{
            qDebug("delete current item");

        });
    }
    void setModelData(QWidget *editor, QAbstractItemModel *model,
                      const QModelIndex &index) const
    {
        QToolButton *deletebtn = static_cast<QToolButton*>(editor);
        QString text = deletebtn->text();
        model->setData(index, text, Qt::EditRole);
    }
    void updateEditorGeometry(QWidget *editor,
                              const QStyleOptionViewItem &option, const QModelIndex &/*index*/) const
    {
        editor->setGeometry(option.rect);
    }
};

class CustModel : public QStandardItemModel
{
    Q_OBJECT
public:
    CustModel(QObject *parent=NULL) : QStandardItemModel(parent) { }
    CustModel(int row, int column, QObject *parent=NULL)
        : QStandardItemModel(row, column, parent) { }
    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const
    {
        if( Qt::TextAlignmentRole == role )
            return Qt::AlignCenter;
        return QStandardItemModel::data(index, role);
    }

};

namespace Ui {
class FormSetting;
}
class FormSetting : public QWidget
{
    Q_OBJECT
signals:
    void onClose(bool,QMap<int,QString>);
public:
    explicit FormSetting(QMap<int,QString>_mapGroupIp,QWidget *parent = 0);
    ~FormSetting();

private:
    Ui::FormSetting *ui;
    void setupGroupToTable();

    QMap<int, QString> m_mapGroupIp;
    bool _IsAddDate;
    QStringList ipList;

protected:
    void closeEvent(QCloseEvent *event);
    bool eventFilter(QObject *, QEvent *);

private slots:
    void AddItem();
};

#endif // FORMSETTING_H
